package com.example.quarkus.resource;

import com.example.quarkus.entity.Emp;
import com.example.quarkus.service.EmpService;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.jboss.resteasy.annotations.jaxrs.PathParam;

/**
 * @author: Hu Shan
 * @date: 2023/3/23 20:13
 * @description: TODO
 */

@Path("/emp")
@Slf4j
//@Produces(MediaType.APPLICATION_JSON)
public class EmpResource {
    @Inject
    EmpService empService;

    @Path("/{id}")
    @GET
    public Response getEmpInfo(@PathParam int id) {
        log.info("getEmpInfo " + id);
        return Response.ok(empService.getEmpById(id)).build();
    }

    @Path("/testredis/{id}")
    @GET
    public String testRedis(@PathParam int id) {
        log.info("testRedis " + id);
        empService.setEmpByIdToRedis(id);
        return "insert redis success";
    }

    /**
     * 测试缓存
     * @param id
     * @return
     */
    @Path("/testCache/{id}")
    @GET
    public Emp testCache(@PathParam int id) {
        log.info("testCache " + id);
        Emp empForCache = empService.getEmpForCache(id);
        return empForCache;
    }

    @Path("/insert")
    @POST
    public String insertEmp(@Valid Emp emp) {
        log.info("insertEmp " + emp);
        empService.insertEmp(emp);
        return "insert success";
    }

    @Path("/update")
    @PUT
    public String updateEmp(@RequestBody Emp emp) {
        log.info("updateEmp " + emp);
        if (empService.updateEmp(emp) == 0) {
            return "fail";
        }
        return "success";
    }

    @Path("/delete/{id}")
    @DELETE
    public String deleteEmp(@PathParam int id) {
        log.info("deleteEmp " + id);
        if (empService.deleteEmp(id) == 0) {
            return "fail";
        }
        return "success";
    }
}
